<template>
	<view>
		<Navbar></Navbar>
		<u-tabs-swiper ref="tabs" active-color="#B3723E" bg-color="transparent" :is-scroll="false" :current="tabsData.current" :list="tabsData.list" @change="tabChange"></u-tabs-swiper>
		<view class="u-m-t-30 u-m-l-30 u-m-r-30">
			<GroupBox ref="friendBox" :list="list" :groupType="5" :recordType="tabsData.current" @addPerson="addPerson" @callMobile="callMobile" @goDetail="goDetail"></GroupBox>
		</view>
		<Loadmore :status="status"></Loadmore>
		
		<Modal :show="modal.show" :confirmText="modal.confirm" :modalContent="modal.content"
			:showCancel="modal.showCancel" @confirm="modalConfirm" @cancel="modal.show = false"></Modal>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Loadmore from '@/components/Loadmore.vue'
	import GroupBox from '@/components/GroupBox.vue'
	import Modal from '@/components/Modal.vue'
	import reachBottom from '@/mixins/reachBottom.js'
	import { userExhibitionRecordList, userExhibitionStatus, businessCardApply, singleIntegral, userInfo } from '@/network/api.js'
	import { call, presidentClass, PresidentLevel, TeaStatus } from '@/utils/index.js'
	export default {
		mixins: [reachBottom],
		components: {
			Navbar,
			Loadmore,
			GroupBox,
			Modal
		},
		data() {
			return {
				companyId: 0,
				tabsData:{
					current: 0,
					list: [{name:'访问'},{name:'咨询'}]
				},
				list: [],
				/* 弹出提示 */
				modal: {
					show: false,
					showCancel: true,
					confirm: '确定',
					content: ''
				},
				fromUserId: '',
				status: 'loadmore',
			}
		},
		onLoad() {
			this.onGetUserRecord()
		},
		methods: {
			async onGetUserRecord(){
				const res = await userExhibitionStatus({userid: uni.getStorageSync("user_id")})
				this.companyId = res.data.company_id
				this.onGetRecord()
			},
			async onGetRecord(){
				const params = {
					user_id: uni.getStorageSync("user_id"),
					company_id: this.companyId,
					type: this.tabsData.current == 0 ? 1 : 2,
					page: this.page,
					limit: this.pageSize
				}
				const res = await userExhibitionRecordList(params)
				res.data.list.map(item=>{
					item.name = item.name === null ? '未注册用户' : item.name
					item.president_status = parseInt(item.president_status)
					item.president_class = presidentClass(item.president_status)
					item.president_name = this.getPresidentName(item.partner_status,item.president_status)
					this.list.push(item)
				})
				if(res.data.list.length < 10) this.status = 'nomore'
			},
			tabChange(val){
				this.tabsData.current = val
				this.page = 1
				this.list.length = 0
				this.onGetRecord()
			},
			goDetail(id){
				uni.navigateTo({
					url: "/teaBureau/teaPersonDetail/teaPersonDetail?id=" + id
				})
			},
			callMobile(val) {
				call(val)
			},
			addPerson(id) {
				this.fromUserId = id
				const params = {
					key: "ExchangeCards",
					type: 0
				}
				singleIntegral(params).then(res => {
					
					this.modal.content = `交换名片，对方同意后交换成功`
					this.modal.show = true
				})
			},
			getPresidentName(status) {
        return TeaStatus(status1, status2)  
				// return PresidentLevel(status)
			},
			modalConfirm() {
				this.modal.show = false
				userInfo({
					user_id: uni.getStorageSync("user_id")
				}).then(res => {
					let integral = res.data.integral // 用户积分
					const params = {
						key: "ExchangeCards",
						type: 0
					}
					singleIntegral(params).then(singleRes => {
						let singleIntegral = singleRes.data.value
						if (parseInt(integral) < parseInt(singleIntegral)) {
							uni.showToast({
								title: "积分不足",
								icon: "none"
							})
						} else {
							const applyParams = {
								user_id: uni.getStorageSync("user_id"),
								from_user_id: this.fromUserId
							}
							businessCardApply(applyParams).then(applyRes => {
								uni.showToast({
									title: `${applyRes.msg}`,
									icON: "none"
								})
							})
						}
					})
				})
			},
			loadData() {
				if (this.status == 'nomore') return
				this.page++
				this.status = 'loading'
				this.onGetRecord()
			}
		}
	}
</script>

<style scoped>
	.message_tag {
		background: #2C76ED;
		color: #fff;
		display: inline-block;
		font-size: 24rpx;
		padding: 5rpx 10rpx;
		border-radius: 10rpx;
	}
</style>